package com.yld.car.market;

import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;

public class RegisterCompanyActivity extends BaseActivity implements
		OnItemClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.register_base_list);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);

		showBackButton();
		Button localButton = (Button) findViewById(R.id.rightButton);
		// localButton.setVisibility(View.VISIBLE);
		// localButton.setText("完成");
		// localButton.setOnClickListener(completeOnClickListener);
		// ((TextView) findViewById(R.id.title)).setText("公司注册");
		list1 = (ListView) findViewById(R.id.itemList);
		list1.setOnItemClickListener(this);
		// RegisterCompanyAdapter adapter = new RegisterCompanyAdapter(this);
		// list1.setAdapter(adapter);
	}

	private String name = "";
	private String coname = "";
	private String address = "";
	private String remark = "";
	private String tel = "";
	private String contacter = "";
	private String city = "";
	private ListView list1;

	private void completeState() {
		if (RegisterCompanyActivity.this.telNumber.trim().equals("")) {
			RegisterCompanyActivity.this.showDialogFinish("手机号不能为空", false);
			return;
		}
		if (RegisterCompanyActivity.this.companyName.trim().equals("")) {
			RegisterCompanyActivity.this.showDialogFinish("公司名称不能为空", false);
			return;
		}
		if (RegisterCompanyActivity.this.companyPrincipal.trim().equals("")) {
			RegisterCompanyActivity.this.showDialogFinish("公司负责人不能为空", false);
			return;
		}
		if (RegisterCompanyActivity.this.contactWay.trim().equals("")) {
			RegisterCompanyActivity.this.showDialogFinish("联系方式不能为空", false);
			return;
		}

		if (RegisterCompanyActivity.this.companyOwnAddress.trim().equals("")) {
			RegisterCompanyActivity.this.showDialogFinish("公司所在地不能为空", false);
			return;
		}
		if (RegisterCompanyActivity.this.companyPort.trim().equals("")) {
			RegisterCompanyActivity.this.showDialogFinish("公司所在港口不能为空", false);
			return;
		}
		if (RegisterCompanyActivity.this.companyAddress.equals("")) {
			RegisterCompanyActivity.this.showDialogFinish("公司地址不能为空", false);
			return;
		}

		if (RegisterCompanyActivity.this.companyIntro.trim().equals("")) {
			RegisterCompanyActivity.this.showDialogFinish("公司简介不能为空", false);
			return;
		}

		new RegisterTask().execute("");

	}

	View.OnClickListener completeOnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			completeState();
		}
	};

	protected void onResume() {

		super.onResume();
		this.companyOwnAddress = mApp.getmProvinceInfo();

		if (companyOwnAddress == null) {
			companyOwnAddress = "";
		}
		this.companyPort = mApp.getmPortInfo();

		if (this.companyPort == null) {
			this.companyPort = "";
		}
		this.city = mApp.getmCityInfo();
		if (this.city == null) {
			this.city = "";
		}
		RegisterCompanyAdapter registerCompanyAdapter = new RegisterCompanyAdapter(
				this);
		list1.setAdapter(registerCompanyAdapter);
	}

	private String telNumber = "";// 手机号码
	private String companyName = "";// 公司名称
	private String companyPrincipal = "";// 负责人
	private String contactWay = "";// 联系方式
	private String companyAddress = "";// 公司地址
	private String companyOwnAddress = "";// 公司所有地
	private String companyPort = "";// 公司港口
	private String companyIntro = "";// 公司简介

	class RegisterCompanyAdapter extends BaseAdapter {
		private Context context;
		private LayoutInflater mInflater;
		private TextWatcher textWatcher1 = new TextWatcher() {
			public void afterTextChanged(Editable paramEditable) {
			}

			public void beforeTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
			}

			public void onTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
				telNumber = paramCharSequence.toString();
			}
		};
		private TextWatcher textWatcher2 = new TextWatcher() {
			public void afterTextChanged(Editable paramEditable) {
			}

			public void beforeTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
			}

			public void onTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
				companyName = paramCharSequence.toString();
			}
		};
		private TextWatcher textWatcher3 = new TextWatcher() {
			public void afterTextChanged(Editable paramEditable) {
			}

			public void beforeTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
			}

			public void onTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
				companyPrincipal = paramCharSequence.toString();
			}
		};
		private TextWatcher textWatcher4 = new TextWatcher() {
			public void afterTextChanged(Editable paramEditable) {
			}

			public void beforeTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
			}

			public void onTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
				contactWay = paramCharSequence.toString();
			}
		};
		private TextWatcher textWatcher5 = new TextWatcher() {
			public void afterTextChanged(Editable paramEditable) {
			}

			public void beforeTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
			}

			public void onTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
				companyAddress = paramCharSequence.toString();
			}
		};
		private TextWatcher textWatcher6 = new TextWatcher() {
			public void afterTextChanged(Editable paramEditable) {
			}

			public void beforeTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
			}

			public void onTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
				companyOwnAddress = paramCharSequence.toString();
			}
		};

		private TextWatcher textWatcher7 = new TextWatcher() {
			public void afterTextChanged(Editable paramEditable) {
			}

			public void beforeTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
			}

			public void onTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
				companyPort = paramCharSequence.toString();
			}
		};

		private TextWatcher textWatcher8 = new TextWatcher() {
			public void afterTextChanged(Editable paramEditable) {
			}

			public void beforeTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
			}

			public void onTextChanged(CharSequence paramCharSequence,
					int paramInt1, int paramInt2, int paramInt3) {
				companyIntro = paramCharSequence.toString();
			}
		};

		public RegisterCompanyAdapter(Context paramContext) {
			this.mInflater = LayoutInflater.from(paramContext);
			this.context = paramContext;
		}

		public int getCount() {
			return 7;
		}

		public Object getItem(int paramInt) {
			return "";
		}

		public long getItemId(int paramInt) {
			return paramInt;
		}

		public View getView(int paramInt, View paramView,
				ViewGroup paramViewGroup) {
			View localView = this.mInflater.inflate(
					R.layout.base_list_items_input, null);
			EditText localEditText = (EditText) localView
					.findViewById(R.id.itemUserName);

			/*
			 * 参数一：telphone:注册手机号 参数二：compName:公司名称 参数三：compPeople:公司负责人
			 * 参数四：compTelphone:联系电话（要提示多个好嘛以，号分隔） 参数五：compAddress:公司地址
			 * 参数六：proviCity:公司所在地（省-市的方法 例如：四川-成都,系统绑定省市，用户自行选择）
			 * 参数七：compPort:公司所在港口(调用车源模块中查询港口的方法：GetCarAllPorts()查询绑定,供用户选择)
			 * 参数八：compIntro:公司简介
			 */
			if (paramInt == 0) {
				localEditText.setHint("注册手机号");
				localEditText.setInputType(InputType.TYPE_CLASS_PHONE);
				localEditText.addTextChangedListener(this.textWatcher1);
			}

			if (paramInt == 1) {
				localEditText.setHint("公司名称");
				localEditText.setInputType(InputType.TYPE_CLASS_TEXT);
				localEditText.addTextChangedListener(this.textWatcher2);
				return localView;
			}
			if (paramInt == 2) {
				localEditText.setHint("公司负责人");
				localEditText.setInputType(InputType.TYPE_CLASS_TEXT);
				localEditText.addTextChangedListener(this.textWatcher3);
				return localView;
			}
			if (paramInt == 5) {
				localEditText.setHint("联系方式，多个号码用逗号分隔");
				localEditText.setInputType(InputType.TYPE_CLASS_TEXT);
				localEditText.addTextChangedListener(this.textWatcher4);
				return localView;
			}
			if (paramInt == 4) {

				localEditText.setHint("公司地址");
				localEditText.setInputType(InputType.TYPE_CLASS_TEXT);
				localEditText.addTextChangedListener(this.textWatcher5);
				return localView;
			}
			if (paramInt == 3) {
				// localEditText.setHint("公司所在地");
				// localEditText.setInputType(InputType.TYPE_CLASS_TEXT);
				// localEditText.addTextChangedListener(this.textWatcher6);
				//
				// return localView;

				localView = mInflater.inflate(R.layout.register_list, null);
				final View localView1 = localView.findViewById(R.id.item1);
				TextView textView = (TextView) localView1
						.findViewById(R.id.itemContent);
				textView.setHintTextColor(context.getResources()
						.getColorStateList(R.color.font_gray_light_2));
				textView.setHint("选择省份");
				textView.setTextColor(context.getResources().getColorStateList(
						R.color.font_gray_light_2));

				final View localView2 = localView.findViewById(R.id.item2);
				TextView textView2 = (TextView) localView2
						.findViewById(R.id.itemContent);
				textView2.setHintTextColor(context.getResources()
						.getColorStateList(R.color.font_gray_light_2));
				textView2.setHint("选择城市");
				textView2.setTextColor(context.getResources()
						.getColorStateList(R.color.font_gray_light_2));

				final View localView3 = localView.findViewById(R.id.item3);
				TextView textView3 = (TextView) localView3
						.findViewById(R.id.itemContent);
				textView3.setHintTextColor(context.getResources()
						.getColorStateList(R.color.font_gray_light_2));
				textView3.setHint("选择港口");
				textView3.setTextColor(context.getResources()
						.getColorStateList(R.color.font_gray_light_2));
				String tmp = "";
				if (companyOwnAddress.equals("")) {
					tmp = "";
				} else {
					tmp = companyOwnAddress;
				}
				textView.setText(tmp);

				if ("重庆市".equals(tmp) || "北京市".equals(tmp) || "天津市".equals(tmp)
						|| "上海市".equals(tmp)) {

					textView2.setText(tmp);

				}

				cityState(textView2, tmp);

				if (companyPort.equals("")) {
					tmp = "";
				} else {
					tmp = companyPort;
				}
				textView3.setText(tmp);

				localView1.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {

						localView1
								.setBackgroundResource(R.drawable.register_common_selector);

						Intent intent = new Intent();
						intent.setClass(RegisterCompanyActivity.this,
								PersonProvinceActivity.class);
						startActivity(intent);
						mApp.setmCityInfo(null);
					}
				});

				localView2.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {

						if ("".equals(companyOwnAddress)) {

							Toast.makeText(RegisterCompanyActivity.this,
									"请确定省份", Toast.LENGTH_SHORT).show();
							return;
						}
						if ("重庆市".equals(companyOwnAddress)
								|| "北京市".equals(companyOwnAddress)
								|| "天津市".equals(companyOwnAddress)
								|| "上海市".equals(companyOwnAddress)) {

						} else {

							Intent intent = new Intent();
							intent.setClass(RegisterCompanyActivity.this,
									PersonCityActivity.class);
							// 传递省市的名称
							intent.putExtra("name", companyOwnAddress);
							startActivity(intent);
						}

						localView2
								.setBackgroundResource(R.drawable.register_common_selector);

					}
				});

				localView3.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {

						localView3
								.setBackgroundResource(R.drawable.register_common_selector);
						Intent intent = new Intent();
						intent.setClass(RegisterCompanyActivity.this,
								SelectorPortActivity.class);
						startActivity(intent);

					}
				});

				localView3
						.setBackgroundResource(R.drawable.register_common_selector);

				return localView;
			}

			// if (paramInt == 6) {
			//
			// // localEditText.setHint("公司所在港口");
			// // localEditText.setInputType(InputType.TYPE_CLASS_TEXT);
			// // localEditText.addTextChangedListener(this.textWatcher7);
			// // return localView;
			//
			// localView = mInflater.inflate(R.layout.base_list_items_simple,
			// null);
			// TextView textView = (TextView) localView
			// .findViewById(R.id.itemContent);
			// textView.setHintTextColor(context.getResources()
			// .getColorStateList(R.color.font_gray_light_2));
			//
			// textView.setHint("公司所在港口");
			// textView.setTextColor(context.getResources().getColorStateList(
			// R.color.font_gray_light_2));
			// textView.setInputType(InputType.TYPE_CLASS_TEXT);
			//
			// String tmp = "";
			// if (companyPort.equals("")) {
			// tmp = "";
			// } else {
			// tmp = companyPort;
			// }
			// textView.setText(tmp);
			// // localEditText.addTextChangedListener(this.textWatcher7);
			// localView
			// .setBackgroundResource(android.R.drawable.list_selector_background);
			// return localView;
			// }
			if (paramInt == 6) {

				// localEditText.setHint("公司简介");
				// localEditText.setInputType(InputType.TYPE_CLASS_TEXT);
				// localEditText.addTextChangedListener(this.textWatcher8);
				// return localView;

				localView = mInflater.inflate(
						R.layout.base_list_items_input_large, null);
				localEditText = (EditText) localView
						.findViewById(R.id.itemContent);
				localEditText.setHint("公司简介");
				localEditText.setInputType(InputType.TYPE_CLASS_TEXT);
				// localEditText.setText(d.getIntro());
				localEditText.addTextChangedListener(this.textWatcher8);
				return localView;
			}
			return localView;
		}

		public boolean isEnabled(int paramInt) {
			return super.isEnabled(paramInt);
		}
	}

	private class RegisterTask extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {

			/**
			 * DealerEegisterInfo(string telphone, string compName, string
			 * compPeople, string compTelphone, string compAddress, string
			 * proviCity, string compPort, string compIntro)
			 * <telphone>string</telphone> <compName>string</compName>
			 * <compPeople>string</compPeople>
			 * <compTelphone>string</compTelphone>
			 * <compAddress>string</compAddress> <proviCity>string</proviCity>
			 * <compPort>string</compPort> <compIntro>string</compIntro>
			 * 参数一：telphone:注册手机号 参数二：compName:公司名称 参数三：compPeople:公司负责人
			 * 参数四：compTelphone:联系电话（要提示多个好嘛以，号分隔） 参数五：compAddress:公司地址
			 * 参数六：proviCity:公司所在地（省-市的方法 例如：四川-成都,系统绑定省市，用户自行选择）
			 * 参数七：compPort:公司所在港口(调用车源模块中查询港口的方法：GetCarAllPorts()查询绑定,供用户选择)
			 * 参数八：compIntro:公司简介
			 */
			HashMap<String, String> allParams = new HashMap<String, String>();

			String tmp = "";
			if ("重庆市".equals(companyOwnAddress)
					|| "北京市".equals(companyOwnAddress)
					|| "天津市".equals(companyOwnAddress)
					|| "上海市".equals(companyOwnAddress)) {

				tmp = companyOwnAddress;
			} else {
				tmp = companyAddress + "-" + city;
			}

			allParams.put("telphone", telNumber);
			allParams.put("compName", companyName);
			allParams.put("compPeople", companyPrincipal);
			allParams.put("compTelphone", contactWay);
			allParams.put("compAddress", companyAddress);
			allParams.put("proviCity", tmp);
			allParams.put("compPort", companyPort);
			allParams.put("compIntro", companyIntro);
			NetworkProgressUtils utils = NetworkProgressUtils.getInstance();
			publishProgress("");

			Object o = utils.getWebservicesByData(
					ConstantUtils.NAME_SPACE,
					ConstantUtils.ALL_METHROD_NAME.get(24),
					ConstantUtils.NAME_SPACE
							+ ConstantUtils.ALL_METHROD_NAME.get(24),
					ConstantUtils.GET_DEALER_REGISTER_INFO_URL, allParams);

			if (o != null) {
				return o.toString();

			}
			return null;
		}

		@Override
		protected void onProgressUpdate(String... values) {
			showLoading("");
			super.onProgressUpdate(values);
		}

		@Override
		protected void onPostExecute(String result) {

			hideLoading();
			if (result != null) {

				if (result.indexOf("注册成功") >= 0) {

					showDialogFinish("注册成功", true);
				} else if (result.indexOf("已被注册") >= 0) {

					Toast.makeText(RegisterCompanyActivity.this, "此号码已注册，请确认!",
							Toast.LENGTH_SHORT).show();
				}
			}
			super.onPostExecute(result);
		}

	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View v, int position,
			long id) {

		// if (position == 5) {
		//
		// Intent intent = new Intent();
		// intent.setClass(this, PersonProvinceActivity.class);
		// startActivity(intent);
		// // System.out.println("onItemClick...");
		// } else if (position == 6) {
		//
		// Intent intent = new Intent();
		// intent.setClass(this, SelectorPortActivity.class);
		// startActivity(intent);
		//
		// }

	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		mApp.setmPortInfo("");
		mApp.setmProvinceInfo("");
		mApp.setmCityInfo("");
	}

	public void onAction(View v) {

		completeState();
	}

	private void cityState(TextView tv, String name) {

		if (this.city == null || this.city.equals("")) {

			if ("河北省".equals(name)) {
				tv.setText("石家庄市");
			} else if ("山西省".equals(name)) {
				tv.setText("太原市");
			} else if ("内蒙古自治区".equals(name)) {
				tv.setText("呼和浩市");
			} else if ("辽宁省".equals(name)) {
				tv.setText("沈阳市");
			} else if ("吉林省".equals(name)) {
				tv.setText("长春市");
			} else if ("黑龙江省".equals(name)) {
				tv.setText("哈尔滨市");
			} else if ("江苏省".equals(name)) {
				tv.setText("南京市");
			} else if ("浙江省".equals(name)) {
				tv.setText("杭州市");
			} else if ("安徽省".equals(name)) {
				tv.setText("合肥市");
			} else if ("福建省".equals(name)) {
				tv.setText("福州市");
			} else if ("江西省".equals(name)) {
				tv.setText("南昌市");
			} else if ("山东省".equals(name)) {
				tv.setText("济南市");
			} else if ("河南省".equals(name)) {
				tv.setText("郑州市");
			} else if ("湖北省".equals(name)) {
				tv.setText("武汉市");
			} else if ("湖南省".equals(name)) {
				tv.setText("长沙市");
			} else if ("广东省".equals(name)) {
				tv.setText("广州市");
			} else if ("广西壮族自治区".equals(name)) {
				tv.setText("南宁市");
			} else if ("海南省".equals(name)) {
				tv.setText("海口市");
			} else if ("四川省".equals(name)) {
				tv.setText("成都市");
			} else if ("贵州省".equals(name)) {
				tv.setText("贵阳市");
			} else if ("云南省".equals(name)) {
				tv.setText("太原市");
			} else if ("西藏自治区".equals(name)) {
				tv.setText("昆明市");
			} else if ("陕西省".equals(name)) {
				tv.setText("西安市");
			} else if ("甘肃省".equals(name)) {
				tv.setText("兰州市");
			} else if ("青海省".equals(name)) {
				tv.setText("西宁市");
			} else if ("宁夏回族自治区".equals(name)) {
				tv.setText("银川市");
			} else if ("新疆维吾尔自治区".equals(name)) {
				tv.setText("乌鲁木齐市");
			}
		} else {
			tv.setText(city);
		}

	}
}
